package com.example.koizzi.ad20180517_news;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.koizzi.ad20180517_news.adepter.HoriAdater;
import com.example.koizzi.ad20180517_news.adepter.VerticalAdater;
import com.example.koizzi.ad20180517_news.bean.News;
import com.example.koizzi.ad20180517_news.bean.NewsType;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements HoriAdater.OnClickListener {
    @BindView(R.id.recy_Hori)
    RecyclerView recyHori;
    @BindView(R.id.recy_Vertical)
    RecyclerView recyVertical;

    private static final int GET_NEWS_SUCCESS = 1001;

    private List<NewsType> HlistType = new ArrayList<>();
    private HoriAdater horiAdater;
    private VerticalAdater verticalAdater;
    private Handler handler;
    private News news;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toolbar toolbar1 = findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar1);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initType();
        initHadtperView();
        QuerryNews("");
        initHandler();
        initVerticalView();
    }

    private void initVerticalView() {
        LinearLayoutManager layoutManagerV = new LinearLayoutManager(MainActivity.this);
        recyVertical.setLayoutManager(layoutManagerV);
        verticalAdater = new VerticalAdater(MainActivity.this, news);
        recyVertical.setAdapter(verticalAdater);
    }


    private void initHandler() {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                verticalAdater.changeDate(news);
                return false;
            }
        });
    }


    @Override
    public void change(String tid) {
        Log.e("*******", tid);
        QuerryNews(tid);
    }

    private void QuerryNews(String tid) {
        if (tid == null || tid == "") {
            tid = "top";
        }
        String tiyu = "http://v.juhe.cn/toutiao/index?type=" + tid + "&key=44bc47ae628915300ff19d45489dea58";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder().url(tiyu).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(MainActivity.this, "onFailure", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String newsJson = response.body().string();
                try {
                    news = new Gson().fromJson(newsJson, News.class);
                    handler.sendEmptyMessage(GET_NEWS_SUCCESS);
                } catch (IllegalStateException e) {
                    Toast.makeText(MainActivity.this, "网络差", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initHadtperView() {
        LinearLayoutManager layoutManagerH = new LinearLayoutManager(MainActivity.this);
        layoutManagerH.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyHori.setLayoutManager(layoutManagerH);
        horiAdater = new HoriAdater(HlistType, MainActivity.this);
        recyHori.setAdapter(horiAdater);
    }

    private void initType() {
        String tid[] = {"top", "shehui", "guonei", "guoji", "yule", "tiyujunshi", "keji", "caijing", "shishang"};
        String type[] = {"头条", "社会", "国内", "国际", "娱乐", "体育", "科技", "财经", "上海"};
        for (int i = 0; i < type.length; i++) {
            NewsType a = new NewsType(type[i], tid[i]);
            HlistType.add(a);
        }
    }

}
